import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:qaun_ni_dev/utils/request.dart';
import 'package:qaun_ni_dev/utils/toast.dart';
import 'package:qaun_ni_dev/model/admin_model.dart';


class AdminProvide with ChangeNotifier {
  var adminDataProvider;
  List<AdminData> adminDataList = [];

  int page = 1;

  getAdminDataList (context) async {
    var formData = {'currentPage': page};
    await requestPost(context,'/sysUser/list',formData: formData).then((value) {
      var responseData = json.decode(value.toString());
      var tempData = AdminModel.fromJson(responseData);
      adminDataList = tempData.data;
      notifyListeners();
    });
  }

  getAdminDataMoreList (context) async {
    page++;
    print('页码${page}');
    Map<String, dynamic> formData = {'currentPage': page};
    await requestPost(context,'/sysUser/list',formData: formData).then((value) {
      var responseData = json.decode(value.toString());
      print('aaa>>>>>>>${responseData['data'].length >0}');
      if(responseData['data'].length >0) {
        var tempData = AdminModel.fromJson(responseData);
        var tempDatas = tempData.data;
        adminDataList.addAll(tempDatas);
        print('长度${adminDataList.length}');
      } else {
        Toast.toast(context,msg: '已经没有数据了哦');
      }

    });
    notifyListeners();
  }
  // 上拉刷新
  onRefreshList (context) async {
    adminDataList = [];
    page = 1;
    var formData = {'currentPage': 1};
    await requestPost(context,'/sysUser/list',formData: formData).then((value) {
      var responseData = json.decode(value.toString());
      var tempData = AdminModel.fromJson(responseData);
      adminDataList = tempData.data;
    });
    notifyListeners();
  }

  // 更新用户信息
  updateAdminInfo (context,formData) async {
    await requestPost(context,'/sysUser/update',formData:formData).then((value){
      var responseData = json.decode(value.toString());
      if(responseData['code'] == 200) {
        Toast.toast(context,msg: '数据更新成功');
        Navigator.pushNamed(context, '/AdminPagess');
      } else {
        Toast.toast(context,msg: '数据更新失败，请重试');
      }
      print(responseData);
    });

  }

  deleteAdminInfo (context, formData) async {
    print(formData.id);
    var formDatas = {'userId': formData.id};
    await requestPost(context,'/sysUser/del',formData: formDatas).then((value) {
      var responseData = json.decode(value.toString());
      if(responseData['code'] == 200) {
        Toast.toast(context,msg: '数据删除成功');
        Navigator.pushNamed(context, '/AdminPagess');
      } else {
        Toast.toast(context,msg: '数据删除失败，请重试');
      }
      print(responseData);
    });
  }



  deleteAdmin () {
    var aaa = adminDataProvider.toString();
    print(aaa);
    print('删除管理员数据');
    notifyListeners();
  }



  setAdmin (data) {
    this.adminDataProvider = data;
    print('设置管理员的数据');
    notifyListeners();
  }
}